package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.commit.MyThreadLocal;
import com.itheima.commit.R;
import com.itheima.mapper.ShoppingCartMapper;
import com.itheima.pojo.ShoppingCart;
import com.itheima.service.ShoppingCartService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements ShoppingCartService {
    @Override
    public R<List<ShoppingCart>> sel() {
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, MyThreadLocal.getThreadLocal());
        wrapper.orderByAsc(ShoppingCart::getCreateTime);

        List<ShoppingCart> list = this.list(wrapper);
        return R.success(list);
    }

    @Override
    public R<ShoppingCart> add(ShoppingCart shoppingCart) {
        Long threadLocal = MyThreadLocal.getThreadLocal();
        shoppingCart.setUserId(threadLocal);

        Long dishId = shoppingCart.getDishId();

        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, threadLocal);

        if (dishId != null) {
            wrapper.eq(ShoppingCart::getDishId, dishId);
        } else {
            wrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }
        ShoppingCart one = getOne(wrapper);
        if (one != null) {
            Integer number = one.getNumber();
            one.setNumber(number + 1);
            updateById(one);
        } else {
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            save(shoppingCart);
            one = shoppingCart;
        }
        return R.success(one);
    }

    @Override
    public R<String> clean() {
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId,MyThreadLocal.getThreadLocal());
        this.remove(wrapper);
        return R.success("删除成功");
    }

    @Override
    public R<String> sub(ShoppingCart shoppingCart) {
        Long threadLocal = MyThreadLocal.getThreadLocal();
        shoppingCart.setUserId(threadLocal);

        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, threadLocal);

        Long dishId = shoppingCart.getDishId();
        if (dishId != null) {
            wrapper.eq(ShoppingCart::getDishId, dishId);
        } else {
            wrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }

        ShoppingCart one = this.getOne(wrapper);

        if (one.getNumber() > 1) {
            Integer number = one.getNumber();
            one.setNumber(number-1);
            this.updateById(one);
        } else {
            this.removeById(one);
        }
        return R.success("ok");

    }
}
